package Logicamente;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.GridBagLayout;

import javax.swing.JComboBox;

import java.awt.GridBagConstraints;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;

import java.awt.Insets;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.Cursor;
import java.awt.Toolkit;

public class VentanaAgregarPregunta extends JFrame {

	private JPanel contentPane;
	private JTextField txtPregunta;
	private JTextField txtOpcion_1;
	private JTextField txtOpcion_2;
	private JTextField txtOpcion_3;
	private JTextField txtOpcion_4;
	private JComboBox comboBox;
	private JLabel lblOpcionCorrecta;
	private JRadioButton radioButton;
	private JRadioButton radioButton_1;
	private JRadioButton radioButton_2;
	private JRadioButton radioButton_3;
	private JPanel panel_1;
	private JLabel lblVolver;
	private JLabel lblAgregar;
	private JButton button;
	private JButton button_1;
	private VentanaProfesor ventanaPadre;
	
	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the frame.
	 */
	public VentanaAgregarPregunta(VentanaProfesor v) {
		setTitle("Logicamente");
		setIconImage(Toolkit.getDefaultToolkit().getImage(VentanaAgregarPregunta.class.getResource("/Imagenes/L.png")));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ventanaPadre = v;
		setBounds(100, 100, 500, 400);
		contentPane = new JPanelConFondo("/Imagenes/fondo.jpg");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanelConFondo("/Imagenes/fondo_t.png");
		contentPane.add(panel, BorderLayout.CENTER);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 200, 100, 0, 0};
		gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{100.0, 50.0, 50.0, 100.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{100.0, 10.0, 0.0, 10.0, 10.0, 10.0, 10.0, 20.0, 10.0, 100.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		txtPregunta = new JTextField();
		txtPregunta.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				limpiarTexto(e,e.getComponent());
			}
		});
		txtPregunta.setFont(new Font("Dialog", Font.BOLD, 12));
		txtPregunta.setText("Pregunta");
		GridBagConstraints gbc_txtPregunta = new GridBagConstraints();
		gbc_txtPregunta.gridwidth = 2;
		gbc_txtPregunta.insets = new Insets(0, 0, 5, 5);
		gbc_txtPregunta.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtPregunta.gridx = 1;
		gbc_txtPregunta.gridy = 1;
		panel.add(txtPregunta, gbc_txtPregunta);
		txtPregunta.setColumns(10);
		
		lblOpcionCorrecta = new JLabel("Opcion correcta");
		lblOpcionCorrecta.setFont(new Font("DejaVu Sans", Font.BOLD, 12));
		GridBagConstraints gbc_lblOpcionCorrecta = new GridBagConstraints();
		gbc_lblOpcionCorrecta.insets = new Insets(0, 0, 5, 5);
		gbc_lblOpcionCorrecta.gridx = 2;
		gbc_lblOpcionCorrecta.gridy = 2;
		panel.add(lblOpcionCorrecta, gbc_lblOpcionCorrecta);
		
		txtOpcion_1 = new JTextField();
		txtOpcion_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				limpiarTexto(e,e.getComponent());
			}
		});
		txtOpcion_1.setFont(new Font("Dialog", Font.BOLD, 12));
		txtOpcion_1.setText("Opcion 1");
		GridBagConstraints gbc_txtOpcion_1 = new GridBagConstraints();
		gbc_txtOpcion_1.insets = new Insets(0, 0, 5, 5);
		gbc_txtOpcion_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtOpcion_1.gridx = 1;
		gbc_txtOpcion_1.gridy = 3;
		panel.add(txtOpcion_1, gbc_txtOpcion_1);
		txtOpcion_1.setColumns(10);
		
		radioButton = new JRadioButton("");
		radioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionRadioButton(e,0);
			}
		});
		radioButton.setBackground(new Color(255, 102, 0));
		radioButton.setBorder(null);
		GridBagConstraints gbc_radioButton = new GridBagConstraints();
		gbc_radioButton.insets = new Insets(0, 0, 5, 5);
		gbc_radioButton.gridx = 2;
		gbc_radioButton.gridy = 3;
		panel.add(radioButton, gbc_radioButton);
		
		txtOpcion_2 = new JTextField();
		txtOpcion_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				limpiarTexto(e,e.getComponent());
			}
		});
		txtOpcion_2.setFont(new Font("Dialog", Font.BOLD, 12));
		txtOpcion_2.setText("Opcion 2");
		GridBagConstraints gbc_txtOpcion_2 = new GridBagConstraints();
		gbc_txtOpcion_2.insets = new Insets(0, 0, 5, 5);
		gbc_txtOpcion_2.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtOpcion_2.gridx = 1;
		gbc_txtOpcion_2.gridy = 4;
		panel.add(txtOpcion_2, gbc_txtOpcion_2);
		txtOpcion_2.setColumns(10);
		
		radioButton_1 = new JRadioButton("");
		radioButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionRadioButton(e,1);
			}
		});
		radioButton_1.setBorder(null);
		radioButton_1.setBackground(new Color(255, 102, 0));
		GridBagConstraints gbc_radioButton_1 = new GridBagConstraints();
		gbc_radioButton_1.insets = new Insets(0, 0, 5, 5);
		gbc_radioButton_1.gridx = 2;
		gbc_radioButton_1.gridy = 4;
		panel.add(radioButton_1, gbc_radioButton_1);
		
		txtOpcion_3 = new JTextField();
		txtOpcion_3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				limpiarTexto(e,e.getComponent());
			}
		});
		txtOpcion_3.setFont(new Font("Dialog", Font.BOLD, 12));
		txtOpcion_3.setText("Opcion 3");
		GridBagConstraints gbc_txtOpcion_3 = new GridBagConstraints();
		gbc_txtOpcion_3.insets = new Insets(0, 0, 5, 5);
		gbc_txtOpcion_3.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtOpcion_3.gridx = 1;
		gbc_txtOpcion_3.gridy = 5;
		panel.add(txtOpcion_3, gbc_txtOpcion_3);
		txtOpcion_3.setColumns(10);
		
		radioButton_2 = new JRadioButton("");
		radioButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionRadioButton(e,2);
			}
		});
		radioButton_2.setBorder(null);
		radioButton_2.setBackground(new Color(255, 102, 0));
		GridBagConstraints gbc_radioButton_2 = new GridBagConstraints();
		gbc_radioButton_2.insets = new Insets(0, 0, 5, 5);
		gbc_radioButton_2.gridx = 2;
		gbc_radioButton_2.gridy = 5;
		panel.add(radioButton_2, gbc_radioButton_2);
		
		txtOpcion_4 = new JTextField();
		txtOpcion_4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				limpiarTexto(e,e.getComponent());
			}
		});
		txtOpcion_4.setFont(new Font("Dialog", Font.BOLD, 12));
		txtOpcion_4.setText("Opcion 4");
		GridBagConstraints gbc_txtOpcion_4 = new GridBagConstraints();
		gbc_txtOpcion_4.insets = new Insets(0, 0, 5, 5);
		gbc_txtOpcion_4.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtOpcion_4.gridx = 1;
		gbc_txtOpcion_4.gridy = 6;
		panel.add(txtOpcion_4, gbc_txtOpcion_4);
		txtOpcion_4.setColumns(10);
		
		radioButton_3 = new JRadioButton("");
		radioButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionRadioButton(e,3);
			}
		});
		radioButton_3.setBackground(new Color(255, 102, 0));
		radioButton_3.setBorder(null);
		GridBagConstraints gbc_radioButton_3 = new GridBagConstraints();
		gbc_radioButton_3.insets = new Insets(0, 0, 5, 5);
		gbc_radioButton_3.gridx = 2;
		gbc_radioButton_3.gridy = 6;
		panel.add(radioButton_3, gbc_radioButton_3);
		
		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Tiempo maximo de respuesta", "10", "15", "20", "25", "30"}));
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.gridwidth = 2;
		gbc_comboBox.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 1;
		gbc_comboBox.gridy = 8;
		panel.add(comboBox, gbc_comboBox);
		
		panel_1 = new JPanelConFondo("/Imagenes/fondo_t.png");
		contentPane.add(panel_1, BorderLayout.SOUTH);
		GridBagLayout gbl_panel_1 = new GridBagLayout();
		gbl_panel_1.columnWidths = new int[]{0, 0, 0, 0, 0};
		gbl_panel_1.rowHeights = new int[] {10, 0, 0, 10};
		gbl_panel_1.columnWeights = new double[]{100.0, 0.0, 0.0, 100.0, Double.MIN_VALUE};
		gbl_panel_1.rowWeights = new double[]{100.0, 0.0, 100.0, Double.MIN_VALUE};
		panel_1.setLayout(gbl_panel_1);
		
		lblVolver = new JLabel("Volver");
		GridBagConstraints gbc_lblVolver = new GridBagConstraints();
		gbc_lblVolver.insets = new Insets(0, 0, 5, 5);
		gbc_lblVolver.gridx = 1;
		gbc_lblVolver.gridy = 0;
		panel_1.add(lblVolver, gbc_lblVolver);
		
		lblAgregar = new JLabel("Agregar");
		GridBagConstraints gbc_lblAgregar = new GridBagConstraints();
		gbc_lblAgregar.insets = new Insets(0, 0, 5, 5);
		gbc_lblAgregar.gridx = 2;
		gbc_lblAgregar.gridy = 0;
		panel_1.add(lblAgregar, gbc_lblAgregar);
		
		button = new JButton("");
		button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				actionVolver(arg0);
			}
		});
		button.setIcon(new ImageIcon(VentanaAgregarPregunta.class.getResource("/Imagenes/boton_volver_f.png")));
		GridBagConstraints gbc_button = new GridBagConstraints();
		gbc_button.insets = new Insets(0, 0, 5, 5);
		gbc_button.gridx = 1;
		gbc_button.gridy = 1;
		panel_1.add(button, gbc_button);
		
		button_1 = new JButton("");
		button_1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				button_1ActionPerformed(arg0);
			}
		});
		button_1.setIcon(new ImageIcon(VentanaAgregarPregunta.class.getResource("/Imagenes/boton_adelante_f.png")));
		GridBagConstraints gbc_button_1 = new GridBagConstraints();
		gbc_button_1.insets = new Insets(0, 0, 5, 5);
		gbc_button_1.gridx = 2;
		gbc_button_1.gridy = 1;
		panel_1.add(button_1, gbc_button_1);
	}
	
	protected void limpiarTexto(MouseEvent e, Component component) {
		JTextField txt = (JTextField) component;
		if(txt.getText().equalsIgnoreCase("Password")){
			((JPasswordField) txt).setEchoChar('*');
		}
		txt.setText("");
	}
	
	protected boolean camposVacios()
	{
		boolean resultado=false;
		if (txtPregunta.getText().equalsIgnoreCase("Pregunta")|| txtPregunta.getText().equalsIgnoreCase(""))
			resultado = true;
		if (!resultado && (txtOpcion_2.getText().equalsIgnoreCase("Opcion 1")|| txtOpcion_2.getText().equalsIgnoreCase("")))
			resultado = true;
		if (!resultado && (txtOpcion_3.getText().equalsIgnoreCase("Opcion 2")|| txtOpcion_3.getText().equalsIgnoreCase("")))
			resultado = true;
		if (!resultado && (txtOpcion_4.getText().equalsIgnoreCase("Opcion 3")|| txtOpcion_4.getText().equalsIgnoreCase("")))
			resultado = true;
		if (!resultado && (txtOpcion_4.getText().equalsIgnoreCase("Opcion 4")|| txtOpcion_4.getText().equalsIgnoreCase("")))
			resultado = true;
		if (!resultado && comboBox.getSelectedIndex()==0)
			resultado = true;
		if (!resultado && (!radioButton.isSelected() && !radioButton_1.isSelected() && !radioButton_2.isSelected() && !radioButton_3.isSelected()))
			resultado = true;
		return resultado;
	}
	
	protected void button_1ActionPerformed(ActionEvent evt) {
		if (camposVacios())
			JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(this),
					 "Debe completar todos los campos para agregar la pregunta.", 
					"Registro",
					JOptionPane.ERROR_MESSAGE);
		else
		{
		this.setVisible(false);
		ventanaPadre.setVisible(true);
		JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(this),
				 "Su pregunta ha sido creada con exito, sera evaluada por"
				 + "el grupo de trabajo de Logicamente.", 
				"Registro",
				JOptionPane.INFORMATION_MESSAGE);
		this.hide();
		}
	}
	
	protected void actionVolver(ActionEvent arg0) {                   
		this.setVisible(false);
		ventanaPadre.setVisible(true);
		this.hide();
	}
	
	protected void actionRadioButton(ActionEvent e, int i)
	{
		if (i==0)
		{
			radioButton_1.setSelected(false);
			radioButton_2.setSelected(false);
			radioButton_3.setSelected(false);
		}
		else 
			if (i==1)
			{
				radioButton.setSelected(false);
				radioButton_2.setSelected(false);
				radioButton_3.setSelected(false);
			}
			else 
				if (i==2)
				{
					radioButton.setSelected(false);
					radioButton_1.setSelected(false);
					radioButton_3.setSelected(false);
				}
				else 
				{
						radioButton.setSelected(false);
						radioButton_1.setSelected(false);
						radioButton_2.setSelected(false);
				}
	}
}
